<?php

namespace Modules\ShopAdmin\Models;
use Illuminate\Support\Facades\DB;
use Modules\AuthAdmin\Services\QueryService;
use Modules\AuthAdmin\Services\CommonService;
use Modules\AuthAdmin\Models\AuthImage as AuthImageModel;
use Modules\ShopAdmin\Models\ShopConsultEvaluate as ShopConsultEvaluateModel;
class ShopConsult extends BaseApiModel
{
	
	/**
     * @name   状态0和1修改为true或false
     * @param  int  $value
     * @return Boolean 
     */
    public function getStatusAttribute($value)
    {
        return $value==1?true:false;
    }
	/**
     * @name   更新时间为null时返回
     * @param  int  $value
     * @return Boolean 
     */
    public function getUpdatedAtAttribute($value)
    {
        return $value?$value:'';
    }
	/**
	 * @name  咨询评价   一对多
	 */
	 public function evaluate_to()
    {
        return $this->hasMany('Modules\ShopAdmin\Models\ShopConsultEvaluate','consult_id','id');
    }
	/**
	 * @name  列表数据
	 * @param  data  array 搜索数据
	 * @return json 
	 */
    public function list_data($data){
		$model = $this->query();
		$model = (new QueryService())->query_condition($model,$data,'title');	
        $list = $model->select('id','title','author','status','created_at','updated_at','sort','like_number','reading_quantity')
				->withCount(['evaluate_to'])
				->orderBy('sort','asc') 
				->orderBy('id','desc') 
				->paginate($data['pagesize'])
				->toArray();	
		return $this->api_success(['data'=>[
			'list'=>$list['data'],
			'total'=>$list['total']
		]]);		
	}
	/**
	 * @name  编辑页面
	 * @param  id int 咨询ID
	 * @return  JSON
	 */
	public function get_edit($id){
		$data = $this->select('id','title','author','images','sort','reading_quantity','content','like_number')->find($id);		
		$CommonService = new CommonService();
		$http = $CommonService->get_http_type();
		$images_arr = [];
		if($data['images']){
			$images = AuthImageModel::whereIn('id',explode('|',$data['images']))->select('id','open','url')->get()->toArray();
			foreach($images as $k=>$v){
				if($v['open'] == 1){
					$images_arr[] = [
						'id'=>$v['id'],
						'name'=>$http.$v['url'],
						'url'=>$http.$v['url']
					];
				}else{
					$images_arr[] = [
						'id'=>$v['id'],
						'name'=>$v['url'],
						'url'=>$v['url']
					];
				}
			}
		}
		$data['images_arr'] = $images_arr;
		$data['content'] = $CommonService->replace_pic_url($data['content']);
		return $this->api_success([
			'data'=>$data
		]);
	}
	/**
	 * @name  真删除公共方法
	 * @param id int  删除id
	 * @param success_msg string 成功返回数据
	 * @param error_msg string 失败返回数据
	 * @return 返回json数据
	 */
	public function common_destroy($id,$success_msg = '删除成功！',$error_msg = '删除失败！'){
		$arr = ShopConsultEvaluateModel::where(['consult_id'=>$id])->pluck('id');
		DB::beginTransaction();
		try{
			if($arr){
				ShopConsultEvaluateModel::destroy($arr);
			}
			$this->destroy($id);
			DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return $this->api_error(['msg'=>$error_msg]);
		}
		return $this->api_success(['msg'=>$success_msg]);
	}
}
